package helloworld;

import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioHandler;

/**
 * Created by yunqisong on 2017/08/31
 * 此处继承自HelloAbsAioHandler，父类已经完成编码和解码的工作
 */
public class HelloServerAioHandler extends HelloAbsAioHandler implements ServerAioHandler {

    /**
     * 处理消息
     */
    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
        HelloPacket helloPacket = (HelloPacket) packet;
        byte[] body = helloPacket.getBody();
        if (body != null) {
            String str = new String(body, HelloPacket.CHARSET);
            System.out.println("收到消息：" + str);

            HelloPacket resppacket = new HelloPacket();
            resppacket.setBody(("收到了你的消息，你的消息是:" + str + "你是:" + String.valueOf(channelContext)).getBytes(HelloPacket.CHARSET));
            Aio.send(channelContext, resppacket);
        }
        return;
    }

}
